home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Maclife 13
/
MACLIFE13-No-93-1996.ISO.7z
/
MACLIFE13-No-93.ISO
/
Programming
/
CodeWarrior Sample Source
/
MyDemo.(7)
/
MyDemo.c
next >
Wrap
C/C++ Source or Header
|
1996-02-18
|
8KB
|
280 lines
/*------------------------------------------------------------------------------
#
# Macintosh Developer Technical Support
#
# Simple Color QuickDraw Sample Application
#
# SillyBalls
#
# SillyBalls.c - C Source
#
# Copyright ゥ 1988 Apple Computer, Inc.
# All rights reserved.
#
# Versions: 1.0 8/88
#
# Components: SillyBalls.c August 1, 1988
# SillyBalls.make August 1, 1988
#
# This is a very simple sample program that demonstrates how to use Color
# QuickDraw. It is about two pages of code, and does nothing more than open
# a color window and draw randomly colored ovals in the window.
#
# The purpose is to show how to get some initial results with Color QuickDraw.
# It is a complete program and is very short to be as clear as possible.
#
# It does not have an Event Loop. It is not fully functional in the sense that
# it does not do all the things you would expect a well behaved Macintosh
# program to do, like size the window naturally, have an event loop, use menus,
# etc.
#
# See Sample and TESample for the general structure and MultiFinder techniques that
# we recommend that you use when building a new application.
#
------------------------------------------------------------------------------*/
// Version 1.0: 6/2/88
// 7/20/88 DJB Converted to C
//
// purpose To demonstrate a simple color App using Color QuickDraw.
// It draws colored balls in a color window, then uses colored
// text inverted in the ball. The ball location and color is Random.
//
// This program was written by Bo3b Johnson, 1/88.
//
// The inverted Bob text was a Skippy Blair special concept,
// kept for obvious aesthetic reasons.
//MW -cut out some other program descriptions.-
//MW ** Metrowerks note **
// All changed code by Metrowerks is commented by "//MW".
// There is one type of modification to the original source:
// ・ Added argument type and return type to function definitions.
// In order to pass with extended error checking on.
//
// 8/31/93 JA
#include <Types.h>
#include <Memory.h>
#include <Quickdraw.h>
#include <Fonts.h>
#include <Events.h>
#include <Menus.h>
#include <Windows.h>
#include <TextEdit.h>
#include <Dialogs.h>
#include <OSUtils.h>
#include <ToolUtils.h>
#include <SegLoad.h>
/* Constants */
#define BallWidth 20
#define BallHeight 20
#define BobSize 8 /* Size of text in each ball */
/* Globals */
Rect windRect;
/* Prototypes */
void Initialize(void);
void NewBall(void);
void EventLoop(void); // ゼロから始めるCodeWarrior (6)
void DoAbout(void); // ゼロから始めるCodeWarrior (7)
//
// Main body of program SillyBalls
//
//MW specified argument and return type.
void main(void)
{
Initialize();
EventLoop();
// do {
// NewBall();
// } while (!Button());
}
//
// Initialize everything for the program, make sure we can run
//
MenuHandle menu1,menu2;
//MW specified argument and return type.
void Initialize(void)
{
WindowPtr mainPtr;
OSErr error;
SysEnvRec theWorld;
//
// Test the computer to be sure we can do color.
// If not we would crash, which would be bad.
// If we canユt run, just beep and exit.
//
error = SysEnvirons(1, &theWorld);
if (theWorld.hasColorQD == false) {
SysBeep(50);
ExitToShell(); /* If no color QD, we must leave. */
}
/* Initialize all the needed managers. */
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
//
// To make the Random sequences truly random, we need to make the seed start
// at a different number. An easy way to do this is to put the current time
// and date into the seed. Since it is always incrementing the starting seed
// will always be different. Donユt for each call of Random, or the sequence
// will no longer be random. Only needed once, here in the init.
//
GetDateTime((unsigned long*) &qd.randSeed);
SetMenuBar(GetNewMBar(128));
DrawMenuBar();
//
// Make a new window for drawing in, and it must be a color window.
// The window is full screen size, made smaller to make it more visible.
//
windRect = qd.screenBits.bounds;
InsetRect(&windRect, 50, 50);
mainPtr = NewCWindow(nil, &windRect, "¥pBob Land", true, documentProc,
(WindowPtr) -1, false, 0);
SetPort(mainPtr); /* set window to current graf port */
TextSize(BobSize); /* smaller font for drawing. */
}
//
// NewBall: make another ball in the window at a random location and color.
//
//MW -specified argument and return type.-
void NewBall(void)
{
RGBColor ballColor;
Rect ballRect;
long int newLeft,
newTop;
//
// Make a random new color for the ball.
//
ballColor.red = Random();
ballColor.green = Random();
ballColor.blue = Random();
//
// Set that color as the new color to use in drawing.
//
RGBForeColor (&ballColor);
//
// Make a Random new location for the ball, that is normalized to the window size.
// This makes the Integer from Random into a number that is 0..windRect.bottom
// and 0..windRect.right. They are normalized so that we don't spend most of our
// time drawing in places outside of the window.
//
newTop = Random(); newLeft = Random();
newTop = ((newTop+32767) * windRect.bottom)/65536;
newLeft = ((newLeft+32767) * windRect.right)/65536;
SetRect(&ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
//
// Move pen to the new location, and paint the colored ball.
//
MoveTo(newLeft, newTop);
PaintOval (&ballRect);
//
// Move the pen to the middle of the new ball position, for the text
//
MoveTo(ballRect.left + BallWidth/2 - BobSize,
ballRect.top + BallHeight/2 + BobSize/2 -1);
//
// Invert the color and draw the text there. This wonユt look quite right in 1 bit
// mode, since the foreground and background colors will be the same.
// Color QuickDraw special cases this to not invert the color, to avoid
// invisible drawing.
//
InvertColor(&ballColor);
RGBForeColor(&ballColor);
DrawString("¥pBob");
}
void EventLoop(void) // ゼロから始めるCodeWarrior (6)
{
EventRecord event;
Boolean quit = false;
long selectID, menuID, itemID;
do {
if (WaitNextEvent(everyEvent, &event, 10, 0)) {
switch(event.what) {
case mouseDown:
if (selectID = MenuSelect(event.where)) {
menuID = HiWord(selectID);
itemID = LoWord(selectID);
if (menuID == 129 && itemID == 1)
quit = true;
else if (menuID == 128 && itemID == 1) // ゼロから始めるCodeWarrior (7)
DoAbout(); // ゼロから始めるCodeWarrior (7)
HiliteMenu(0);
}
break;
case keyDown:
break;
default:
NewBall();
break;
}
}
} while (!quit);
}
void DoAbout(void) // ゼロから始めるCodeWarrior (7)
{
WindowPtr aboutPtr, oldPort;
Rect aboutWindRect, thePictRect, pictRect;
PicHandle thePicture;
short theWidth, theHeight;
// about画面のPICTリソース(ID=128)と同じ大きさとし,ウィンドウの表示位置を(100,100)とする
SetRect(&aboutWindRect, 100,100,256+100,128+100);
// ウィンドウのタイトルをアプリケーション名とする
aboutPtr = NewCWindow(nil, &aboutWindRect, "¥pデモでもデモ について", true, documentProc,
(WindowPtr) -1, false, 0);
GetPort(&oldPort); // 直前のグラフポートを保存する
SetPort(aboutPtr); // グラフポートをabout画面のグラフポートへ変更する
thePicture = GetPicture(128); // about画面のPICTリソース(ID=128)を読み込み,
thePictRect = (**(thePicture)).picFrame; // PICTの表示位置を求める
theWidth = thePictRect.right - thePictRect.left;
theHeight = thePictRect.bottom - thePictRect.top;
SetRect(&pictRect, 0, 0, theWidth, theHeight);
DrawPicture(thePicture, &pictRect); // PICTをabout画面に表示する
ReleaseResource( (Handle) thePicture ); // PICTリソースのメモリ領域を解放する
do { // クリックされるまでabout画面を表示し続ける
} while (!Button());
DisposeWindow(aboutPtr); // about画面用のメモリ領域を解放する=ウィンドウを消す
SetPort(oldPort); // グラフポートを直前のグラフポートへ戻す
}